# coding: utf-8
from pathlib import Path
from typing import Union

from pydantic import BaseModel, Field

from .public import read, save, load


class CacheModel(BaseModel):
    cache_path: Union[Path, str] = Field(description='缓存文件路径')
    result: str = Field('Success', description='执行结果')
    total: Union[int, str] = Field(0, description='总数')
    passed: Union[int, str] = Field(0, description='通过数')
    failed: Union[int, str] = Field(0, description='失败数')
    error: Union[int, str] = Field(0, description='错误数')
    skipped: Union[int, str] = Field(0, description='跳过数')
    started_time: str = Field('', description='开始时间')
    elapsed: Union[int, str] = Field(0, description='耗时')
    link: str = Field('', description='报告链接')

    def read(self):
        return read(self)

    def save(self):
        """
        保存设置文件
        :return:
        """
        return save(self, self.cache_path)

    def load(self) -> 'CacheModel':
        """
        加载设置文件
        :return:
        """
        data = load(self.cache_path)
        if data:
            return CacheModel(**data)
        else:
            self.save()
            return self
